//
//  TOHotViewController.m
//  A段项目音乐播放
//
//  Created by dllo on 15/5/28.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//

#import "TOHotViewController.h"
#import "firstModel.h"
#import "UIImageView+WebCache.h"
#import <AFNetworking/AFNetworking.h>
#import "newSongModel.h"
#import "NewSongTableViewCell.h"
#import "SingerModel.h"
#import "playViewController.h"
#import "PendulumView.h"
#import "firsPageViewController.h"
@interface TOHotViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,retain)NSMutableDictionary *dic ;
@property (nonatomic ,retain)UITableView *tableView;
@property (nonatomic ,retain)NSMutableDictionary *dic1;
@property (nonatomic ,retain)NSMutableArray *array;
@property (nonatomic ,retain)NSMutableArray *modelArray;
@property (nonatomic,retain)NSMutableDictionary *dic2;
@end

@implementation TOHotViewController
@synthesize rightSwipeGestureRecognizer;

- (void)dealloc
{
    [_tableView release];
    [_array release ];
    [_dic1 release];
    [_dic release];
    [_modelArray release];
    [super dealloc];
    
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    
    self =[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        self.address = [NSMutableString string];
        self.dic =[NSMutableDictionary dictionary];
        self.dic1 =[NSMutableDictionary dictionary];
        self.array =[NSMutableArray array];
        self.modelArray =[NSMutableArray array];
        self.dic2 =[NSMutableDictionary dictionary];
    }
    
    return self;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
    [self createRequest];
    UIColor *ballColor = [UIColor colorWithRed:0.47 green:0.60 blue:0.89 alpha:1];
    PendulumView *pendulum = [[PendulumView alloc] initWithFrame:self.view.bounds ballColor:ballColor];
    [self.view addSubview:pendulum];
    UIBarButtonItem *button =[[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(button:)];
    
    [self.navigationItem setLeftBarButtonItem:button];
    
    self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
    
}
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
{
    /* 设置翻页动画 */
    if (sender.direction == UISwipeGestureRecognizerDirectionRight)
    {
        firsPageViewController * one = [[firsPageViewController alloc]init];
        
        [self.navigationController popViewControllerAnimated:YES];
        
        [one release];
    }
}
-(void)button:(id)but

{
    firsPageViewController * one = [[firsPageViewController alloc]init];
    
    [self.navigationController popViewControllerAnimated:YES];
    
    [one release];
    
    
}
// 请求第一步网络
-(void)createRequest
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer =[AFCompoundResponseSerializer serializer];
    NSString *str =[NSString stringWithFormat:@"http://v1.ard.q.itlily.com/share/user_timeline?msg_ids=%@&app=ttpod&v=v7.9.1.2015050518&uid=&mid=iPhone7%%2C1&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=2e8dd0719f865dbd638d826ca3d916340498fd19&idfa=9288002A-5F6B-453C-B9D5-F41531F959F3&utdid=VVnb1XdXglwDAAqEwlDEEN4a&alf=201200&bundle_id=com.ttpod.music",self.model.value];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray *array=[dic objectForKey:@"data"];
        
        NSDictionary *dic2 = [array objectAtIndex:0];
        [self.dic2 setValue:[dic2 objectForKey:@"tweet"] forKey:@"tweet"];
        
        self.dic =[dic2 objectForKey:@"user"];
        
        NSArray *arraa =[dic2 objectForKey:@"songlist"];
        
        for (NSDictionary *dic in arraa)
        {
            
            [self.dic1 addEntriesFromDictionary:dic];
            
        }
        
        NSDictionary *diccc = [array objectAtIndex:0];
        NSArray *aarray = [diccc objectForKey:@"songlist"];
        
        for (NSDictionary *dc in aarray)
            
        {
            NSString *strrr  =[dc objectForKey:@"song_id"];
            NSString *str = [NSString string];
            str = [NSString stringWithFormat:@"%@",strrr];
            
            self.address = [NSMutableString stringWithFormat:@"%@%@,",self.address ,str];
            
        }
        
        [self requestidnet];
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"error=%@",error);
        
    }];
    
}
//请求第二步网络
- (void)requestidnet
{
    AFHTTPRequestOperationManager *manage=[AFHTTPRequestOperationManager manager];
    
    manage.responseSerializer =[AFCompoundResponseSerializer serializer];
    NSString *str1 =[NSString stringWithFormat:@"http://ting.hotchanson.com/songs/downwhite?song_id=%@&app=ttpod&v=v7.9.1.2015050518&uid=&mid=iPhone7%%2C1&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=2e8dd0719f865dbd638d826ca3d916340498fd19&idfa=9288002A-5F6B-453C-B9D5-F41531F959F3&utdid=VVnb1XdXglwDAAqEwlDEEN4a&alf=201200&bundle_id=com.ttpod.music",self.address];
    NSString *strr = [str1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [manage GET:strr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray *array=[dic objectForKey:@"data"];
        for (NSDictionary *dic2 in array)
        {
            SingerModel *model = [[[SingerModel alloc]init] autorelease];
            [model setValue:[dic2 objectForKey:@"singer_name"] forKey:@"singer_name"];
            [model setValue:[dic2 objectForKey:@"album_name"] forKey:@"album_name"];
            [model setValue:[dic2 objectForKey:@"pick_count"] forKey:@"pick_count"];
            [model setValue:[dic2 objectForKey:@"song_name"] forKey:@"song_name"];
            NSArray *arraysss =[dic2 objectForKey:@"audition_list"];
            NSDictionary *dic3 = [NSDictionary dictionary];
            if (arraysss.count != 3 && arraysss.count != 0) {
                dic3 = arraysss[0];
            }else{
                dic3 = arraysss[1];
            }
            [model setValuesForKeysWithDictionary:dic3];
            [self.modelArray addObject:model];
        }
        
        [self createTableView];
        
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"error=%@",error);
        
    }];
    
}

-(void)createTableView
{
    self.tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate =self;
    self.tableView.dataSource =self;
    [self.tableView setSeparatorColor:[UIColor whiteColor]];
    UIImageView *imageview =[[ UIImageView alloc]initWithFrame:CGRectMake(0,20,120,120)];
    [imageview sd_setImageWithURL:[NSURL URLWithString:[self.dic objectForKey:@"pic"]] placeholderImage:nil options:SDWebImageRetryFailed];
    UIImageView *imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,0,180)];
    imageView.image =[UIImage imageNamed:@"30.jpg"];
    UIView *view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 180)];
    view.backgroundColor =[UIColor cyanColor];
    [imageView addSubview:view];
    imageview.userInteractionEnabled =YES;
    [imageview.layer setCornerRadius:60];
    [imageview.layer setMasksToBounds:YES];
    [imageView addSubview:imageview];
    UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(0,140, 150, 40)];
    label.text =[self.dic objectForKey:@"nick_name"];
    label.textColor=[UIColor orangeColor];
    [imageView addSubview:label];
    UILabel *label2 =[[UILabel alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/3+20,[UIScreen mainScreen].bounds.size.width/16,[UIScreen mainScreen].bounds.size.width/2+30,[UIScreen mainScreen].bounds.size.width/2-30)];
    label2.text =[self.dic2 objectForKey:@"tweet"];
    label2.numberOfLines =0;
    label2.textColor=[UIColor orangeColor];
    [imageView addSubview:label2];
    [self.tableView setTableHeaderView:imageView];
    [self.tableView registerClass:[NewSongTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
    
    return [self.modelArray count];
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewSongTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
    SingerModel *model =[self.modelArray objectAtIndex:indexPath.row];
    if (self.modelArray.count == 0) {
        cell.textLabel.text = @"mei";
        
    } else
    {
        [cell setHotsongModel:model];
        
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    
    return 90;
    
}



- (void)didReceiveMemoryWarning
{
    
    [super didReceiveMemoryWarning];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    playViewController *play = [playViewController shareStateOfViewcontroller];
    [play playWithModel:self.modelArray index:indexPath.row];
    
    [self.navigationController pushViewController:play animated:YES];
    
}


@end
